package com.miplus.ccrm.core.controller;

import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JSysUser;
import com.miplus.ccrm.common.PageData;
import com.miplus.ccrm.common.R;
import com.miplus.ccrm.core.controller.vo.credit.ImpResultVo;
import com.miplus.ccrm.core.controller.vo.sto.*;
import com.miplus.ccrm.core.security.JCommonData;
import com.miplus.ccrm.core.service.sto.CreditCockcollectService;
import com.miplus.ccrm.core.service.sto.OnLineService;
import com.miplus.ccrm.core.service.sto.PlAi94ChatService;
import com.miplus.ccrm.core.service.sto.PlRsReturnCollectRecordService;
import com.miplus.ccrm.sms.LineAPIR;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 2-7.项目个性化服务接口
 */
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
@RequestMapping("/sto")
public class StoController {
    @Resource
    private JCommonData jCommonData;
    @Resource
    private CreditCockcollectService cockcollectService;

    @Resource
    private OnLineService onLineService;

    @Resource
    private PlRsReturnCollectRecordService rsReturnCollectRecordService;

    @Resource
    private PlAi94ChatService plAi94ChatService;

    /**
     * 导入公鸡贷
     */
    @PostMapping("/impCockList")
    public R<ImpResultVo> impCockList(@RequestBody @Validated CockImpVo cockImpVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cockcollectService.impCockList(usrid,cockImpVo));
    }

    /**
     * 生成公鸡贷案件
     */
    @PostMapping("/createCockCase")
    public R<CmdResVo> createCockCase(@RequestBody @Validated CrtFiCaseVo crtFiCaseVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cockcollectService.CreateFiCase(usrid,crtFiCaseVo));
    }

    /**
     * 删除公鸡贷批次
     */
    @PostMapping("/deleteCockBatch")
    public R<CmdResVo> deleteCockBatch(@RequestBody @Validated CockDelVo cockDelVo){
        String usrid= jCommonData.getCurrUsrID();
        return R.Success(cockcollectService.DeleteFiCase(usrid,cockDelVo.getBatchid()));
    }

    /**
     * 取公鸡贷案件详细信息
     */
    @GetMapping("/getCockCaseInfo")
    public R<CockCaseVo> getCockCaseInfo(@RequestParam @NotNull Integer ficaseid){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(cockcollectService.getCockCaseInfo(user,ficaseid));
    }


    //联机接口

    /**
     * 还款申请金额验证
     */
    @PostMapping("/repaymentVerification")
    public R<OnLineResVo> repaymentVerification(@RequestBody @Validated OnLineReqVo reqVo){
        return R.Success(onLineService.repaymentVerification(reqVo));
    }


    /**
     * 智慧线上额度刷新
     *
     * 请求:
     * productid 产品编号
     * customernumber 客户编号
     * 返回:
     * 可用额度: AvlblLmt1
     * 已用额度：UsedLmt
     * 批准额度总金额: AprvLmtTotAmt
     * @return
     */
    @PostMapping("/refreshSOFPCreditLimit")
    public R<LineAPIR> refreshSOFPCreditLimit(@RequestBody @Validated SOFPReqVo reqVo){

        return R.Success(onLineService.refreshSOFPCreditLimit(reqVo));
    }

    /**
     * 智慧线上借据明细
     * 请求:
     * ctrSeqNo 合同流水号
     * customernumber 客户编号
     * status 明细状态 2:逾期
     * @return
     */
    @PostMapping("/SOFPLoanLnvoiceInfo")
    public R<LineAPIR> getSOFPLoanLnvoiceInfo(@RequestBody @Validated SOFPReqVo reqVo){

        return R.Success(onLineService.getSOFPLoanLnvoiceInfo(reqVo));
    }

    /**
     * 智慧线上还款计划
     * 请求:
     * customernumber 客户编号
     * loanDblNo 借据号
     * @return
     */
    @PostMapping("/SOFPRepayPlan")
    public R<LineAPIR> getSOFPRepayPlan(@RequestBody @Validated SOFPReqVo reqVo){

        return R.Success(onLineService.getSOFPRepayPlan(reqVo));
    }
    /**
     * 智慧线上还款记录
     * 请求:
     * customernumber 客户编号
     * loanDblNo 借据号
     * @return
     */
    @PostMapping("/SOFPRepaySerial")
    public R<LineAPIR> getSOFPRepaySerial(@RequestBody @Validated SOFPReqVo reqVo){

        return R.Success(onLineService.getSOFPRepaySerial(reqVo));
    }

    /**
     * 智慧线上还款试算
     * 请求:
     * trialTp 试算类型
     * loanDblNo 借据号
     * @return
     */
    @PostMapping("/SOFPTrial")
    public R<LineAPIR> getSOFPTrial(@RequestBody @Validated SOFPReqVo reqVo){
        return R.Success(onLineService.getSOFPTrial(reqVo));
    }


    /**
     * 还款申请
     * @return
     */
    @PostMapping("/repaymentApply")
    public R<CmdResVo> SOFPRepaymentApply(@RequestBody @Validated RepayApplyReqVo reqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(onLineService.repaymentApply(user,reqVo));
    }


    /**
     * 案件实时刷新
     * @return
     */
    @GetMapping("/refreshCaseInfo")
    public R<CmdResVo> refreshCase(@RequestParam @NotNull @Param("creditcaseid") String creditcaseid){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(onLineService.refreshCase(creditcaseid, user.getUsrid()));
    }

    /**
     * 案件实时刷新 批量
     * batchList
     * @return
     */
    @PostMapping("/refreshCaseBatch")
    public R<RefreshCaseBatchReqVo> refreshCaseBatch(@RequestBody @Validated RefreshCaseBatchReqVo  reqVo){
        JSysUser user= jCommonData.getCurrUsr();
        return R.Success(onLineService.refreshCaseBatch(reqVo,user.getUsrid()));
    }


    /**
     * 案件批量扣款
     * @param reqVo
     * @return
     */
    @PostMapping("/deductionBatch")
    public R<CaseDeductionBatchReqVo> caseDeductionBatch(@RequestBody @Validated CaseDeductionBatchReqVo reqVo){
        JSysUser user= jCommonData.getCurrUsr();

        return R.Success(onLineService.caseDeductionBatch(reqVo,user));
    }


    /**
     * 还款结果查询
     * 请求: creditcaseid 案件id
     * applyType 1:融资中台 2:智慧线上
     * @return
     */
    @GetMapping("/repayApplyList")
    public R<List<RepayApplyResVo>> repayApplyList(@RequestParam @NotNull
                                                   @Param("creditcaseid") String creditcaseid,
                                                   @NotNull @Param("applyType") String applyType
    ){
        return R.Success(onLineService.repayApplyList(creditcaseid,applyType));
    }


    /**
     * 融资中台借据明细
     * 请求:
     * mrchId 商户号
     * customernumber 客户编号
     * status 明细状态 2:逾期
     * @return
     */
    @PostMapping("/CLMPLoanLnvoiceInfo")
    public R<LineAPIR> getCLMPLoanLnvoiceInfo(@RequestBody @Validated CLMPReqVo reqVo){
        return R.Success(onLineService.getCLMPLoanLnvoiceInfo(reqVo));
    }
    /**
     * 融资中台还款计划
     * 请求:
     * mrchId 商户号
     * dblNo 借据编号
     * @return
     */
    @PostMapping("/CLMPRepayPlan")
    public R<LineAPIR> getCLMPRepayPlan(@RequestBody @Validated CLMPReqVo reqVo){
        return R.Success(onLineService.getCLMPRepayPlan(reqVo));
    }
    /**
     * 融资中台还款记录
     * 请求:
     * mrchId 商户号
     * customernumber 客户编号
     * dblNo 借据编号
     * productId 产品编号
     * @return
     */
    @PostMapping("/CLMPRepaySerial")
    public R<LineAPIR> getCLMPRepaySerial(@RequestBody @Validated CLMPReqVo reqVo){
        return R.Success(onLineService.getCLMPRepaySerial(reqVo));
    }

    /**
     * 融资中台额度刷新
     * 请求:
     * mrchId 商户号
     * customernumber 客户编号
     * productId 产品编号
     * @return
     */
    @PostMapping("/refreshCLMPCreditLimit")
    public R<LineAPIR> refreshCLMPCreditLimit(@RequestBody @Validated CLMPReqVo reqVo){
        return R.Success(onLineService.refreshCLMPCreditLimit(reqVo));
    }

    /**
     * 荣数催记历史
     * @param reqVo
     * @return
     */
    @PostMapping("/rsCollectRecordPage")
    public R<PageData<RsReturnCollectRecordResVo>> rsCollectRecordPage (@RequestBody @Validated RsReturnCollectRecordReqVo reqVo){
        return R.SuccessPage(rsReturnCollectRecordService.rsCollectRecordPage(reqVo));
    }

    /**
     * 94对话记录
     * @param callid
     * @return
     */
    @GetMapping("/94QuyIvrDialogue")
    public R<List<Ai94ChatResVo>> quyIvrDialogue(@RequestParam @NotNull String callid) {

        return R.Success(plAi94ChatService.ai94ChatList(callid));
    }

}
